<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META NAME="generator" CONTENT="http://www.shopex.cn">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<LINK REL="stylesheet" TYPE="text/css" HREF="images/style.css">
<TITLE>LAMP/LNMP环境搭建</TITLE>
</HEAD>
<BODY>

<DIV CLASS="header" ID="header">
<H1>LAMP/LNMP环境搭建</H1>
<H2>author  :   lazyboy, lazyboy.fan@gmail.com</H2>
<H3>since   :   2010-06-22 15:22:39</H3>
</DIV>

<DIV CLASS="toc" ID="toc">
  <OL>
  <LI><A HREF="#toc1">系统要求</A>
  <LI><A HREF="#toc2">Linux</A>
    <UL>
    <LI><A HREF="#toc3">2.1. CGI方式(推荐Linux+Nginx+Mysql+PHP组合)</A>
      <UL>
      <LI><A HREF="#toc4">2.1.1. Mysql</A>
      <LI><A HREF="#toc5">2.1.2. Apache</A>
      <LI><A HREF="#toc6">2.1.3. PHP</A>
      <LI><A HREF="#toc7">2.1.4. mod_fcgid</A>
      <LI><A HREF="#toc8">2.1.5. Nginx</A>
      </UL>
    <LI><A HREF="#toc9">2.2. MOD方式</A>
      <UL>
      <LI><A HREF="#toc10">2.2.1. Mysql</A>
      <LI><A HREF="#toc11">2.2.2. Apache</A>
      <LI><A HREF="#toc12">2.2.3. PHP</A>
      </UL>
    <LI><A HREF="#toc13">2.3. Python安装</A>
    <LI><A HREF="#toc14">2.4. Erlang安装</A>
    <LI><A HREF="#toc15">2.5. Mongodb安装</A>
    <LI><A HREF="#toc16">2.6. Rabbitmq Server安装</A>
    </UL>
  <LI><A HREF="#toc17">Windows</A>
  </OL>

</DIV>
<DIV CLASS="body" ID="body">
<A NAME="toc1"></A>
<H1>1. 系统要求</H1>
<UL>
<LI>Linux 2.6+ 内核，本文中的Linux操作系统为Red Hat Enterprise Linux 5.4(32Bit)
<P></P>
<LI>安装软件及版本如下(源码在/usr/local/src目录下)：
 <UL>
 <LI><B><CODE>httpd-2.2.15.tar.bz2</CODE></B>
 <LI><B><CODE>mod_fcgid-2.3.5.tar.bz2</CODE></B>
 <LI><B><CODE>mysql-5.1.35.tar.gz</CODE></B>
 <LI><B><CODE>php-5.2.6.tar.bz2</CODE></B>
 <LI><B><CODE>php-5.2.6-fpm-0.5.9.diff.gz</CODE></B>
 <LI><B><CODE>pcre-8.02.tar.bz2</CODE></B>
 <LI><B><CODE>libevent-1.4.14-stable.tar.gz</CODE></B>
 <LI><B><CODE>nginx-1.0.10.tar.gz</CODE></B>
 <LI><B><CODE>ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz</CODE></B>
 <LI><B><CODE>eaccelerator-0.9.6.tar.bz2</CODE></B>
 <LI><B><CODE>memcached-1.4.5.tar.gz</CODE></B>
 <LI><B><CODE>memcache-2.2.5-stable.tgz</CODE></B>
 <LI><B><CODE>proftpd-1.3.2.tar.gz</CODE></B>
 <LI><B><CODE>otp_src_R14B04.tar.gz</CODE></B>
 </UL>
</UL>

<HR NOSHADE SIZE=5>
<A NAME="toc2"></A>
<H1>2. Linux</H1>
<A NAME="toc3"></A>
<H2>2.1. CGI方式(推荐Linux+Nginx+Mysql+PHP组合)</H2>
<A NAME="toc4"></A>
<H3>2.1.1. Mysql</H3>
<H4>2.1.1.1. 编译&amp;&amp;安装Mysql</H4>
<OL>
<LI>解压&amp;&amp;进入Mysql源码目录
<PRE>
tar -zxf mysql-5.1.35.tar.gz
cd mysql-5.1.35
</PRE>
<P></P>
<LI>编译&amp;&amp;安装(Mysql源码目录下执行)
<PRE>
./configure --prefix=/usr/local/mysql \
--sysconfdir=/etc \
--enable-thread-safe-client \
--enable-assembler \
--enable-local-infile \
--with-charset=utf8 \
--with-extra-charsets=gbk \
--with-pthread \
--with-mysqld-ldflags=-all-static \
--with-client-ldflags=-all-static \
--with-embedded-server \
--with-plugins=max \
--with-unix-socket-path=/tmp/mysql.sock \
--disable-shared
make
make install
</PRE>
<LI>编译指令说明：
 <OL>
 <LI>--sysconfdir=/etc Mysql配置文件存放路径
 <LI>--enable-thread-safe-client 以线程方式编译客户端
 <LI>--enable-assembler 使用一些字符函数的汇编版本(优化性能)
 <LI>--enable-local-infile 启用LOAD DATA LOCAL INFILE命令(默认:disabled)
 <LI>--with-charset=utf8 默认字符集：utf8
 <LI>--with-extra-charsets=all 默认字符集外的其他字符集
 <LI>--with-pthread 强制使用pthread库(posix线程库)
 <LI>--with-mysqld-ldflags=-all-static 以纯静态方式编译服务端(静态链接提高13%性能)
 <LI>--with-client-ldflags=-all-static 以纯静态方式编译客户端(静态链接提高13%性能)
 <LI>--with-plugins=max Mysql的插件(包含存储引擎) 有效值：none max max-no-ndb all
 <LI>--with-unix-socket-path=/tmp/mysql.sock(提高性能)
 <LI>--disable-shared 禁用动态库
 <P></P>
 </OL>
<LI>Mysql编译时间比较长,耐心等待。如果一切顺利,现在就要设置Mysql自启动脚本
<PRE>
cp support-files/mysql.server /etc/init.d/mysqld -v
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 345  mysqld on
chkconfig --level 0126  mysqld off
</PRE>
现在还不能启动Mysql服务,因为还没有初始化Mysql
</OL>

<H4>2.1.1.2. 配置Mysql</H4>
<OL>
<LI>添加Mysql的运行用户和组
<PRE>
groupadd mysql
useradd mysql -g mysql -s /bin/false
</PRE>
<P></P>
<LI>初始化Mysql数据库(Mysql源码目录下执行)
<PRE>
./scripts/mysql_install_db --user=mysql --datadir=/opt/mysql
</PRE>
<P></P>
<LI>拷贝Mysql配置文件到/etc目录下(Mysql源码目录下执行)
<PRE>
cp support-files/my-medium.cnf /etc/my.cnf -v
</PRE>
<LI>修改配置文件/etc/my.cnf 在[mysql]配置项添加datadir路径
<PRE>
vim /etc/my.cnf

[mysqld]
datadir     = /opt/mysql
</PRE>
<LI>修改Mysql相关目录权限，以便Mysql正常启动和正确读写
<PRE>
chown mysql.mysql /usr/local/mysql/ -R
chown mysql.mysql /opt/mysql/ -R
</PRE>
<P></P>
<LI>启动Mysql服务
<PRE>
service mysqld start
</PRE>
<P></P>
<LI>查看Mysql服务是否启动
<PRE>
ps -ef|grep mysqld
</PRE>
得到下面所示 就说明已经正常启动
<IMG ALIGN="middle" SRC="images/mysql.png" BORDER="0" ALT="">
<P></P>
<LI>Mysql的一些后续工作
</OL>

<UL>
<LI>建立符号链接 方便使用
<PRE>
ln -sv /usr/local/mysql/bin/mysqladmin /sbin/mysqladmin
ln -sv /usr/local/mysql/bin/mysql /sbin/mysql
</PRE>
<P></P>
<LI>设置Mysql超级管理员密码
<PRE>
mysqladmin -uroot password "root"
</PRE>
<P></P>
<LI>删除用于本机匿名连接的空密码帐号
<PRE>
mysql -uroot -p
mysql&gt; use mysql;
mysql&gt; delete from user where Password="";
mysql&gt; flush privileges;
mysql&gt; \q
</PRE>
</UL>

<HR NOSHADE SIZE=1>
<P></P>
<A NAME="toc5"></A>
<H3>2.1.2. Apache</H3>
<H4>2.1.2.1. 编译apache前续操作</H4>
<P>
解压&amp;&amp;进入apache源码目录
</P>
<PRE>
tar -jxf httpd-2.2.15.tar.bz2
cd httpd-2.2.15
</PRE>
<P>
<B><CODE>如果不准备装subversion 下面这一步可以跳过</CODE></B>
</P>
<P>
删除原有configure文件，重新建立configure文件
</P>
<PRE>
rm -f configure
rm -f srclib/apr-util/configure
./buildconf --enable-lib64
</PRE>
<P></P>
<H4>2.1.2.2. 编译&amp;&amp;安装apr</H4>
<P>
切换到apr目录
</P>
<PRE>
cd srclib/apr
./configure --prefix=/usr/local/apache/apr-httpd
make
make install
</PRE>
<P></P>
<H4>2.1.2.3. 编译&amp;安装apr-util</H4>
<P>
切换到apr-util目录
</P>
<PRE>
cd ../apr-util/
./configure --prefix=/usr/local/apache/apr-util --with-apr=/usr/local/apache/apr-httpd
make
make install
</PRE>
<P></P>
<H4>2.1.2.4. 编译&amp;&amp;安装apache</H4>
<OL>
<LI>编译&amp;&amp;安装()
<PRE>
cd ../../
./configure --prefix=/usr/local/apache \
--enable-lib64 \
--with-apr=/usr/local/apache/apr-httpd \
--with-apr-util=/usr/local/apache/apr-util \
--with-mpm=prefork \
--enable-dav \
--enable-so \
--enable-rewrite=shared \
--enable-track-vars \
--enable-ssl \
--enable-modules=most \
--enable-mods-shared=most
make
make install
</PRE>
<LI>编译指令说明：
 <OL>
 <LI>--enable-lib64  启用64位的lib库文件
 <LI>--with-apr 指定apr的安装目录
 <LI>--with-apr-util 指定apr-util的安装目录
 <LI>--with-mpm 指定apache以prefork方式运行 (在*nix下比较稳定，速度稍高于worker，相对来说系统开销比worker稍多点)
 <LI>--enable-so 开启DSO性能
 <LI>--enable-modules=most 开启模块 most||all
 <LI>--enable-mods-shared=most 模块编译成DSO most||all
 <LI>--enable-rewrite=shared 将rewrite模块编译成DSO
 <P></P>
 </OL>
<LI>配置apache启动脚本 拷贝apache控制脚本到linux启动目录
<PRE>
cp support/apachectl /etc/init.d/httpd -v
</PRE>
编辑/etc/init.d/httpd文件 在第3行插入以下内容
<PRE>
vim /etc/init.d/httpd
#chkconfig: 345 85 15
#description: Starts And Stops The Apache HTTP Server
</PRE>
<P></P>
<LI>让apache服务随机启动
<PRE>
chmod +x /etc/init.d/httpd
chkconfig --add httpd
chkconfig --level 345 httpd on
chkconfig --level 0126 httpd off
</PRE>
<P></P>
<LI>启动httpd服务
<PRE>
service httpd start
</PRE>
或者
<PRE>
/usr/local/apache/bin/apachectl start
</PRE>
<LI>查看apache是否启动
<PRE>
ps -ef|grep httpd
</PRE>
得到下图所示结果 说明apache正常启动
<P></P>
<IMG ALIGN="middle" SRC="images/apache.png" BORDER="0" ALT="">
</OL>

<HR NOSHADE SIZE=1>
<P></P>
<A NAME="toc6"></A>
<H3>2.1.3. PHP</H3>
<H4>2.1.3.1. 编译PHP前续工作</H4>
<OL>
<LI>编译php需要有gd库，zlib库，openssl库的支持，而gd要依赖freetype、jpeg、png库。一般发行版如Redhat都会已经安装好了的。少数精简太厉害的vps预装的操作系统不一定会按照有，所以我们先检查我们需要的库
 <OL>
 <LI>检查zlib(gd、gzip用)
<PRE>
find /usr/ -name zlib.h
</PRE>
 <LI>检查freetype(gd用)
<PRE>
find /usr/ -name freetype.h
</PRE>
 <LI>检查jpeg头(gd用)
<PRE>
find /usr/ -name jpeglib.h
</PRE>
 <LI>检查png头(gd用)
<PRE>
find /usr/ -name png.h
</PRE>
 <LI>检查openssl头(php用)
<PRE>
find /usr/ -name ssl.h
</PRE>
搜索的结果中include前的部分就是我们在编译php是时指定用的，例如
 <P></P>
<IMG ALIGN="middle" SRC="images/png.h.png" BORDER="0" ALT="">
 <P></P>
 </OL>
<LI>安装缺少的依赖库
 <OL>
 <LI>编译&amp;&amp;安装zlib
<PRE>
tar zxvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure --prefix=/usr/
make
make install
</PRE>
 <P></P>
 <LI>编译&amp;&amp;安装libpng
<PRE>
tar zxvf libpng-1.2.14.tar.gz
cd libpng-1.2.14
cp scripts/makefile.linux makefile
sed -i 's/\/usr\/local/\/usr/' makefile
make
make install
</PRE>
 <LI>编译&amp;&amp;安装freetype
<PRE>
tar -zvxf freetype-2.1.10.tar.gz
cd freetype-2.1.10
mkdir -p /usr/local/freetype
./configure --prefix=/usr/
make
make install
</PRE>
 <LI>编译&amp;&amp;安装jpeg
<PRE>
tar zxvf jpegsrc.v6b.tar.gz
./configure --prefix=/usr
make
make install
make install-lib
</PRE>
 </OL>
</OL>

<H4>2.1.3.2. 编译&amp;&amp;安装PHP(FastCGI方式)</H4>
<P>
解压PHP
</P>
<PRE>
tar -jxf php-5.2.6.tar.bz2
</PRE>
<P>
将php-fpm以补丁的形式加入到php的源码包内
</P>
<PRE>
gzip -cd php-5.2.6-fpm-0.5.9.diff.gz|patch -d php-5.2.6 -p1
</PRE>
<P></P>
<OL>
<LI>编译&amp;&amp;安装
<PRE>
cd php-5.2.6
./configure --prefix=/usr/local/php \
--with-config-file-path=/etc \
--enable-fastcgi \
--enable-force-cgi-redirect \
--enable-fpm \
--with-mysql=/usr/local/mysql \
--with-freetype-dir \
--with-zlib-dir \
--with-jpeg-dir \
--with-png-dir \
--with-gd \
--enable-mbstring \
--with-libxml-dir \
--with-gettext \
--enable-sockets \
--with-curl \
--enable-gd-native-ttf \
--enable-zip \
--with-pear
make
make install
</PRE>
<P></P>
<LI>编译指令说明：
 <OL>
 <LI>--prefix 指定php的安装目录(默认：/usr/local)
 <LI>--with-config-file-path php配置文件php.ini的存放路径(默认：PREFIX/lib)
 <LI>--enable-fastcgi 开启fastcgi模块
 <LI>--enable-force-cgi-redirect 如果用apache运行cgi 开启该参数
 <LI>--with-mysql 指定mysql的安装路径
 <LI>--with-gd 开启gd库
 <LI>--enable-mbstring 开启多字节字符串模块(可选参数)
 <LI>--with-libxml-dir libxml2安装路径
 <LI>--with-gettext gettext模块(可选参数)
 <LI>--enable-fpm  fastcgi process manage(fastcgi进程管理模块)
 <LI>--enable-sockets 开启sockets支持(可选参数)
 <LI>--with-curl 开启curl扩展(可选参数)
 <LI>--enable-gd-native-ttf  开启ttf扩展
 <LI>--enable-zip 开启zip扩展
 <LI>--with-pear 开启PEAR [默认：PREFIX/lib/php]
 <P></P>
 </OL>
<LI>拷贝php配置文件到--with-config-file-path设置的目录
<PRE>
cp php.ini-dist /etc/php.ini -v
cd ../
</PRE>
<LI>修改php.ini文件 时区/扩展目录位置
<PRE>
vim /etc/php.ini
date.timezone = "Asia/Shanghai"
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613"
</PRE>
<LI>修改系统配置文件
<PRE>
vim /etc/sysctl.conf
kernel.shmmax = 134217728
</PRE>
<LI>执行命令 使系统配置生效
<PRE>
/sbin/sysctl -p
</PRE>
<LI>创建www用户和组
<PRE>
groupadd www
useradd www -g www -s /bin/false
</PRE>
<LI>创建php-fpm配置文件
<PRE>
mv -v /usr/local/php/etc/php-fpm.conf /usr/local/php/etc/php-fpm.conf.bak
vim /usr/local/php/etc/php-fpm.conf
</PRE>
<LI>php-fpm文件内容如下
<PRE>
&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
&lt;configuration&gt;
    All relative paths in this config are relative to php's install prefix
    &lt;section name="global_options"&gt;
        Pid file
        &lt;value name="pid_file"&gt;/usr/local/php/logs/php-fpm.pid&lt;/value&gt;

        Error log file
        &lt;value name="error_log"&gt;/usr/local/php/logs/php-fpm.log&lt;/value&gt;

        Log level
        &lt;value name="log_level"&gt;notice&lt;/value&gt;

        When this amount of php processes exited with SIGSEGV or SIGBUS ...
        &lt;value name="emergency_restart_threshold"&gt;10&lt;/value&gt;

        ... in a less than this interval of time, a graceful restart will be initiated.
        Useful to work around accidental curruptions in accelerator's shared memory.
        &lt;value name="emergency_restart_interval"&gt;1m&lt;/value&gt;

        Time limit on waiting child's reaction on signals from master
        &lt;value name="process_control_timeout"&gt;5s&lt;/value&gt;

        Set to 'no' to debug fpm
        &lt;value name="daemonize"&gt;yes&lt;/value&gt;
    &lt;/section&gt;
    &lt;workers&gt;
        &lt;section name="pool"&gt;
            Name of pool. Used in logs and stats.
            &lt;value name="name"&gt;default&lt;/value&gt;

            Address to accept fastcgi requests on.
            Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
            &lt;value name="listen_address"&gt;127.0.0.1:9000&lt;/value&gt;

            &lt;value name="listen_options"&gt;
                Set listen(2) backlog
                &lt;value name="backlog"&gt;-1&lt;/value&gt;

                Set permissions for unix socket, if one used.
                In Linux read/write permissions must be set in order to allow connections from web server.
                Many BSD-derrived systems allow connections regardless of permissions.
                &lt;value name="owner"&gt;&lt;/value&gt;
                &lt;value name="group"&gt;&lt;/value&gt;
                &lt;value name="mode"&gt;0666&lt;/value&gt;
            &lt;/value&gt;

            Additional php.ini defines, specific to this pool of workers.
            &lt;value name="php_defines"&gt;
                &lt;value name="sendmail_path"&gt;/usr/sbin/sendmail -t -i&lt;/value&gt;
                &lt;value name="display_errors"&gt;1&lt;/value&gt;
            &lt;/value&gt;

            Unix user of processes
            &lt;value name="user"&gt;www&lt;/value&gt;

            Unix group of processes
            &lt;value name="group"&gt;www&lt;/value&gt;

            Process manager settings
            &lt;value name="pm"&gt;
                Sets style of controling worker process count.
                Valid values are 'static' and 'apache-like'
                &lt;value name="style"&gt;static&lt;/value&gt;

                Sets the limit on the number of simultaneous requests that will be served.
                Equivalent to Apache MaxClients directive.
                Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
                Used with any pm_style.
                &lt;value name="max_children"&gt;64&lt;/value&gt;

                Settings group for 'apache-like' pm style
                &lt;value name="apache_like"&gt;
                    Sets the number of server processes created on startup.
                    Used only when 'apache-like' pm_style is selected
                    &lt;value name="StartServers"&gt;20&lt;/value&gt;

                    Sets the desired minimum number of idle server processes.
                    Used only when 'apache-like' pm_style is selected
                    &lt;value name="MinSpareServers"&gt;5&lt;/value&gt;

                    Sets the desired maximum number of idle server processes.
                    Used only when 'apache-like' pm_style is selected
                    &lt;value name="MaxSpareServers"&gt;35&lt;/value&gt;
                &lt;/value&gt;
            &lt;/value&gt;

            The timeout (in seconds) for serving a single request after which the worker process will be terminated
            Should be used when 'max_execution_time' ini option does not stop script execution for some reason
            '0s' means 'off'
            &lt;value name="request_terminate_timeout"&gt;0s&lt;/value&gt;

            The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
            '0s' means 'off'
            &lt;value name="request_slowlog_timeout"&gt;0s&lt;/value&gt;

            The log file for slow requests
            &lt;value name="slowlog"&gt;logs/slow.log&lt;/value&gt;

            Set open file desc rlimit
            &lt;value name="rlimit_files"&gt;51200&lt;/value&gt;

            Set max core size rlimit
            &lt;value name="rlimit_core"&gt;0&lt;/value&gt;

            Chroot to this directory at the start, absolute path
            &lt;value name="chroot"&gt;&lt;/value&gt;

            Chdir to this directory at the start, absolute path
            &lt;value name="chdir"&gt;&lt;/value&gt;

            Redirect workers' stdout and stderr into main error log.
            If not set, they will be redirected to /dev/null, according to FastCGI specs
            &lt;value name="catch_workers_output"&gt;yes&lt;/value&gt;

            How much requests each process should execute before respawn.
            Useful to work around memory leaks in 3rd party libraries.
            For endless request processing please specify 0
            Equivalent to PHP_FCGI_MAX_REQUESTS
            &lt;value name="max_requests"&gt;500&lt;/value&gt;

            Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
            Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
            Makes sense only with AF_INET listening socket.
            &lt;value name="allowed_clients"&gt;127.0.0.1&lt;/value&gt;

            Pass environment variables like LD_LIBRARY_PATH
            All $VARIABLEs are taken from current environment
            &lt;value name="environment"&gt;
                &lt;value name="HOSTNAME"&gt;$HOSTNAME&lt;/value&gt;
                &lt;value name="PATH"&gt;/usr/local/bin:/usr/bin:/bin&lt;/value&gt;
                &lt;value name="TMP"&gt;/tmp&lt;/value&gt;
                &lt;value name="TMPDIR"&gt;/tmp&lt;/value&gt;
                &lt;value name="TEMP"&gt;/tmp&lt;/value&gt;
                &lt;value name="OSTYPE"&gt;$OSTYPE&lt;/value&gt;
                &lt;value name="MACHTYPE"&gt;$MACHTYPE&lt;/value&gt;
                &lt;value name="MALLOC_CHECK_"&gt;2&lt;/value&gt;
            &lt;/value&gt;
        &lt;/section&gt;
    &lt;/workers&gt;
&lt;/configuration&gt;
</PRE>
<P></P>
<LI>启动php-cgi进程，监听127.0.0.1的9000端口，进程数为64，用户为www
<PRE>
/usr/local/php/sbin/php-fpm start
</PRE>
注：/usr/local/php/sbin/php-fpm -h 可以查看其他参数，包括：start|stop|quit|restart|reload|logrotate，修改php.ini后不重启php-cgi，重新加载配置文件使用reload。
<P></P>
至此apache、php、mysql安装完成,要让apache以cgi运行php,还需要安装apache的一个扩展模块mod_fcgid
</OL>

<A NAME="toc7"></A>
<H3>2.1.4. mod_fcgid</H3>
<UL>
<LI>安装mod_fcgid模块有2种方式
 <OL>
 <LI><B><CODE>单独安装</CODE></B>： 如果apache已经安装,推荐选用该方式
 <LI><B><CODE>解压到apache的源码包下安装</CODE></B>： 适用于apache还没有安装的情况
 </OL>
</UL>

<H4>2.1.4.1. 编译&amp;&amp;安装(mod_fcgid)</H4>
<UL>
<LI><B>单独安装</B>
<PRE>
tar -jxvf mod_fcgid-2.3.5.tar.bz2
cd mod_fcgid-2.3.5
APXS=/usr/local/apache/bin/apxs ./configure.apxs
</PRE>
<P></P>
查看mod_fcgid模块是否生成
<PRE>
ls -lt /usr/local/apache/modules/mod_fcgid.so
vim /usr/local/apache/conf/httpd.conf
</PRE>
看看是否有这句 若有 说明fcgid模块已经打开,反之,请检查上述步骤是否有问题
<PRE>
LoadModule fcgid_module modules/mod_fcgid.so
</PRE>
<P></P>
<LI><B>解压到apache的源码包下安装</B>
<P></P>
解压
<PRE>
tar -jxvf mod_fcgid-2.3.5.tar.bz2
</PRE>
拷贝所有文件到apache的源码目录下
<PRE>
cp -rv mod_fcgid-2.3.5 httpd-2.2.15
</PRE>
切换到httpd源码目录 并重新生成configure文件
<PRE>
cd httpd-2.2.15
./buildconf
</PRE>
下面的步骤跟上述安装apache是一样的 唯一不同的是在配置参数的时候 多加上一句 –-enable-fcgid 即
<PRE>
./configure –-enable-fcgid ...(后面是原有的配置参数)
</PRE>
OK 不出问题 apache已经支持cgi模式 下面开始配置apache
</UL>

<H4>2.1.4.2. 配置apache</H4>
<P>
打开apache配置文件
</P>
<PRE>
vim /usr/local/apache/conf/httpd.conf
</PRE>
<P>
找到
</P>
<PRE>
DirectoryIndex index.html
</PRE>
<P>
修改为
</P>
<PRE>
DirectoryIndex index.html index.htm index.php
</PRE>
<P></P>
<P>
修改发布根目录Directory段
</P>
<PRE>
&lt;Directory "/usr/local/apache/htdocs"&gt;
    AddHandler fcgid-script .php
    Options Indexes FollowSymLinks +ExecCGI
    #/usr/local/php/bin/php-wrapper 是一个shell脚本，请看下面
    FcgidWrapper /usr/local/php/bin/php-wrapper .php
    DirectoryIndex index.html index.htm index.php
    Order allow,deny
    Allow from all
&lt;/Directory&gt;

</PRE>
<P>
<B>php-wrapper</B> shell脚本代码
</P>
<PRE>
#!/bin/sh
# Set desired PHP_FCGI_* environment variables.
# Example:
# PHP FastCGI processes exit after 500 requests by default.
PHP_FCGI_MAX_REQUESTS=10000
export PHP_FCGI_MAX_REQUESTS
#这里替换你php-cgi的路径
exec /usr/local/php/bin/php-cgi
</PRE>
<P>
现在切换到/usr/local/php/bin/目录下
</P>
<PRE>
cd /usr/local/php/bin/
vim php-wrapper
</PRE>
<P>
将上述shell代码加进去 保存
然后赋予php-wrapper执行权限
</P>
<PRE>
chmod +x php-wrapper
</PRE>
<P>
重启apache
</P>
<PRE>
service httpd restart
</PRE>
<P></P>
<A NAME="toc8"></A>
<H3>2.1.5. Nginx</H3>
<OL>
<LI>使用nginx url_rewrite功能 需要先安装pcre(正则表达式引擎) 官网地址：<A HREF="http://www.pcre.org/" target="_blank">http://www.pcre.org/</A> 本系统使用pcre-8.20.tar.bz2
<PRE>
cd /usr/local/src
tar -jxvf pcre-8.20.tar.bz2
cd cd pcre-8.20
./configure
make &amp;&amp; make install
cd ../
</PRE>
<LI>下载nginx源码包
<PRE>
wget -c http://nginx.org/download/nginx-1.0.10.tar.gz
</PRE>
<LI>安装nginx (注：<I>--with-pcre是pcre库的源码路径 --with-http_ssl_module需要openssl库,若系统没有,请下载安装并执行--with-openssl的源码包路径</I>)
<PRE>
tar -zxvf nginx-1.0.10.tar.gz
cd nginx-1.0.10
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.20
make &amp;&amp; make install
cd ../
</PRE>
<LI>创建nginx配置文件
<PRE>
mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak -v
vim /usr/local/nginx/conf/nginx.conf
</PRE>
<LI>nginx.conf内容如下
<PRE>
#使用的用户和组
user  www www;

#派生进程数
worker_processes 4;

#错误日志
error_log  logs/nginx_error.log  crit;

#pid存放路径
pid        /tmp/nginx.pid;

#指定文件描述符数量
worker_rlimit_nofile 51200;

events {
    #使用的网络I/O模型 linux推荐采用epoll模型 FreeBSD推荐采用kqueue模型
    use epoll;
    #允许的连接数
    worker_connections  51200;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #设置使用的字符集 如果一个网站使用多种字符集 请不要随便设置 应让程序员在html代码中使用meta标签设置
    #charset utf-8;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/nginx_access.log  main;

    #server_names_hash_backet_size 128;
    client_header_buffer_size 32k;
    #large_client_buffer_headers 4 32k;

    #设置客户端能够上传的文件大小
    client_max_body_size 8m;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    keepalive_timeout  60;
    #keepalive_timeout  0;

    #fastcgi参数设置
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

   #开启gzip压缩
   gzip  on;
   gzip_min_length 1k;
   gzip_buffers 4 16k;
   gzip_http_version 1.1;
   gzip_comp_level 2;
   gzip_types text/plain application/x-javascript text/css application/xml;
   gzip_vary on;

   #limit_zone crawler $binary_remote_addr 10m;

    server {
        #监听地址及端口
        listen       80;
        server_name  192.168.61.157;
        #网站发布根目录
        root   /data/httpd;
        #默认搜索文件
        index  index.html index.htm index.php;
        #自动列目录
        autoindex on;

        #处理日志存放路径
        access_log  logs/nginx_access.log  main;
        #charset koi8-r;

        location ~ .*\.(jpg|gif|jpeg|png|bmp|swf)$ {
            expires 30d;
        }

        location ~ .*\.(js|css)?$ {
            expires 1h;
        }
        #php-cgi 监听
        location ~ .*\.php?$ {
            include fastcgi.conf;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
        }

        #rewrite 重写
        #if (-d $request_filename)
        #{
        #     rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent
        #}

        #limit_conn crawler 20;

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #error_page   500 502 503 504  /50x.html;
        #location = /50x.html {
        #    root   /var/www/html/nginx;
        #}

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include vhosts/*.conf;
}
</PRE>
<LI>创建网站发布目录
<PRE>
mkdir -p /data/httpd
</PRE>
<LI>启动nginx
<PRE>
ulimit -SHn 51200
/usr/local/nginx/sbin/nginx
</PRE>
<LI>配置开机自动启动Nginx+PHP
<PRE>
vim /etc/rc.local
</PRE>
末尾添加
<PRE>
ulimit -SHn 51200
/usr/local/php/sbin/php-fpm start
/usr/local/nginx/sbin/nginx
</PRE>
<LI>优化linux系统内核参数
<PRE>
vim /etc/sysctl.conf
</PRE>
末尾添加
<PRE>
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024  65535
</PRE>
使配置立即生效
<PRE>
/sbin/sysctl -p
</PRE>
<P></P>
<LI>创建nginx控制脚本(针对nginx0.8.x之前的版本)
<PRE>
vim /etc/init.d/nginx
</PRE>
<LI>nginx控制脚本内容如下
<PRE>
#!/bin/bash
#
# 适用范围：nginx版本低于0.8.x
# chkconfig: - 85 15
# description: Nginx is a World Wide Web server.
# processname: nginx

nginx=/usr/local/nginx/sbin/nginx
conf=/usr/local/nginx/conf/nginx.conf

case $1 in
    start)
        echo -n "Starting Nginx"
        $nginx -c $conf
        echo " done"
    ;;
    stop)
        echo -n "Stopping Nginx"
        ps aux | grep nginx | grep master | grep -v "grep" | awk '{print $2}' | xargs kill -QUIT
        echo " done"
    ;;
    test)
        $nginx -t -c $conf
    ;;
    reload)
        echo -n "Reloading Nginx"
        ps aux | grep nginx | grep master | grep -v "grep" | awk '{print $2}' | xargs kill -HUP
        echo " done"
    ;;
    restart)
        $0 stop
        $0 start
    ;;
    show)
        ps aux|grep nginx
    ;;
    *)
        echo -n "Usage: $0 {start|restart|reload|stop|test|show}"
    ;;
esac
</PRE>
</OL>

<H4>2.1.5.1. 平滑重启nginx</H4>
<OL>
<LI>修改配置文件后，检查配置文件是否正确
<PRE>
/usr/local/nginx/sbin/nginx -t
</PRE>
若出现下面2行，说明配置文件正确
<PRE>
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
</PRE>
<LI>Nginx 0.8.x之前版本 使用上面/etc/init.d/nginx控制脚本
<PRE>
/etc/init.d/nginx reload
</PRE>
<LI>Nginx 0.8.x及最新版本
<PRE>
/usr/local/nginx/sbin/nginx -s reload
</PRE>
<LI>查看 /usr/local/nginx/sbin/nginx -s 还有其他参数 stop|quit|reopen|reload
</OL>

<H4>2.1.5.2. 编写每天定时切割Nginx日志的脚本</H4>
<OL>
<LI>创建脚本 /usr/local/nginx/sbin/cut_nginx_log.sh
<PRE>
vim /usr/local/nginx/sbin/cut_nginx_log.sh
</PRE>
<LI>cut_nginx_log.sh 脚本内容如下
<PRE>
#!/bin/bash
# This script run at 00:00

# The Nginx logs path
logs_path="/usr/local/nginx/logs/"

mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}nginx_access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/nginx_access_$(date -d "yesterday" +"%Y%m%d").log
ps aux | grep nginx | grep master | grep -v "grep" | awk '{print $2}' | xargs kill -USR1
echo 'cut log done.'
</PRE>
<LI>设置脚本可执行权限
<PRE>
chmod +x /usr/local/nginx/sbin/cut_nginx_log.sh
</PRE>
<LI>设置定时脚本 每天凌晨00:00切割nginx访问日志
<PRE>
crontab -e
</PRE>
末尾添加一下内容
<PRE>
# Nginx日志分割定时脚本
00 00 * * * /bin/sh /usr/local/nginx/sbin/cut_nginx_log.sh
</PRE>
</OL>

<A NAME="toc9"></A>
<H2>2.2. MOD方式</H2>
<A NAME="toc10"></A>
<H3>2.2.1. Mysql</H3>
<A NAME="toc11"></A>
<H3>2.2.2. Apache</H3>
<A NAME="toc12"></A>
<H3>2.2.3. PHP</H3>
<A NAME="toc13"></A>
<H2>2.3. Python安装</H2>
<OL>
<LI>下载python源码包 下载页面：<A HREF="http://www.python.org/getit/" target="_blank">http://www.python.org/getit/</A>
<PRE>
cd /usr/local/src
wget -c http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tar.bz2
tar -jxvf Python-2.7.2.tar.bz2
cd Python-2.7.2
./configure
make &amp;&amp; make install
</PRE>
<LI>如果系统有老版本的python 本文中之前的老版本是2.4 需将新版本做软连接
<PRE>
#rm -f /usr/bin/python
ln -sfv /usr/local/bin/python /usr/bin/python
</PRE>
<LI>安装setuptools(安装后就会产生easy_install) 下载页面：<A HREF="http://pypi.python.org/pypi/setuptools#files" target="_blank">http://pypi.python.org/pypi/setuptools#files</A> 选择setuptools-0.6c11.tar.gz
<PRE>
cd /usr/local/src
tar -zxvf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python setup.py install
cd ../
</PRE>
<LI>安装simplejson 使用easy_install
<PRE>
easy_install simplejson
</PRE>
</OL>

<A NAME="toc14"></A>
<H2>2.4. Erlang安装</H2>
<OL>
<LI>下载erlang源码包(erlang需要先安装python和simplejson) 下载页面：<A HREF="http://www.erlang.org/download.html" target="_blank">http://www.erlang.org/download.html</A>
<LI>解压并安装erlang <I><B>建议不指定前缀--prefix</B>   [编译可能会出错缺少JDK,本文不用java,可以使用--without-javac禁用]</I>
<PRE>
cd /usr/local/src
wget -c http://www.erlang.org/download/otp_src_R14B04.tar.gz
tar -zxvf otp_src_R14B04.tar.gz
cd otp_src_R14B04
./configure
make &amp;&amp; make install
cd ../
</PRE>
<LI>安装erlang man手册 下载页面：<A HREF="http://www.erlang.org/download.html" target="_blank">http://www.erlang.org/download.html</A>
<PRE>
wget -c http://www.erlang.org/download/otp_doc_man_R14B04.tar.gz
tar -zxvf otp_doc_man_R14B04.tar.gz
mv man /usr/local/erlang/ -v
</PRE>
</OL>

<A NAME="toc15"></A>
<H2>2.5. Mongodb安装</H2>
<OL>
<LI>下载mongodb二进制包 [Linux 32-bit] 下载页面：<A HREF="http://www.mongodb.org/downloads" target="_blank">http://www.mongodb.org/downloads</A>
<PRE>
cd /usr/local/src
wget -c http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.1.tgz
tar -zxvf mongodb-linux-i686-2.0.1.tgz
mv mongodb-linux-i686-2.0.1 /usr/local/mongodb -v
</PRE>
<LI>启动mongodb服务 监听27017端口
<PRE>
/usr/local/mongodb/bin/mongod --journal --port 27017 --dbpath /opt/mongodb --fork --logpath /opt/mongodb/mongodb.log --oplogSize 64 --logappend
</PRE>
<LI>设置开机自启动
<PRE>
vim /etc/rc.local
</PRE>
末尾添加
<PRE>
/usr/local/mongodb/bin/mongod --journal --port 27017 --dbpath /opt/mongodb --fork --logpath /opt/mongodb/mongodb.log --oplogSize 64 --logappend
</PRE>
</OL>

<A NAME="toc16"></A>
<H2>2.6. Rabbitmq Server安装</H2>
<OL>
<LI>rabbitmq是用erlang语言所写,所以需要先装erlang.  下载rabbitmq server二进制包[Binary] 下载页面：<A HREF="http://www.rabbitmq.com/download.html" target="_blank">http://www.rabbitmq.com/download.html</A>
<PRE>
cd /usr/local/src
wget -c http://www.rabbitmq.com/releases/rabbitmq-server/v2.7.0/rabbitmq-server-generic-unix-2.7.0.tar.gz
tar -zxvf rabbitmq-server-generic-unix-2.7.0.tar.gz
mv rabbitmq_server-2.7.0 /usr/local/rabbitmq -v
</PRE>
<LI>做软连接 这样rabbitmq的一些命令就可以直接使用了
<PRE>
ln -sv /usr/local/rabbitmq/sbin/* /usr/local/sbin/
</PRE>
</OL>

<A NAME="toc17"></A>
<H1>3. Windows</H1>
<HR NOSHADE SIZE=1>
<P>
最后修改时间 2011-12-09 14:12:36 Friday
<A HREF="index.t2t">查看源文件</A>
</P>
</DIV>

<!-- html code generated by txt2tags 2.5 (http://open.shopex.cn) -->
<!-- cmdline: txt2tags index.t2t -->
</BODY></HTML>
